#!/usr/bin/Rscript

# This script downloads the 24 QPF published by the CNRFC
# and saves it to the file rainfall.
# The script checks if the value in the file already exists 
# for a day so no duplicate values get saved 
 

path <- "/home/acvmarsh/public_html/inflow/"
path <- "./"
tmpfile <- tempfile()
outfile <- paste(path,"rainfall",sep="")

use.gdata <- TRUE
use.nws.fc <- TRUE
if(use.gdata) library(gdata)
source(paste(path,'get.x.fc.R',sep='/'))
source(paste(path,'get.nws.obs.R',sep='/'))
wget <- "/opt/local/bin/wget"
lat <- 40.80778
lon <- -124.1611
latlon <- paste(lat,lon,sep=',')
nfcdays <- 3

date <- Sys.Date()
dateStr <- format(date,'%Y%m%d')

#-------Get yesterday's observation from NWS
yesterdayObs <- get.nws.obs()
#-------------------------------------------

#-------Get the 24 hour QPF for 3 days 
fc <- if(use.nws.fc) get.nws.fc() else get.grib.fc(degrib.path = path)
#-------------------------------------------


#-------Now read in the existing file and place the new fc
block <- try(read.table(outfile,fill=T,header=T, na.strings = "-9.00"),silent=T)
header <- c("year","mo","dy",paste(nfcdays:1,'day',sep=''),'obs')

if(class(block)=="try-error"){

    cat("outfile does not exist.\n")
    cat('Creating NEW file:',outfile,'\n')
    block <- data.frame(matrix(NA,nfcdays+1,4+nfcdays))
    colnames(block) <- header
    new.date <- Sys.Date() + -1:(nfcdays-1)
    block[,1] <- as.integer(format(new.date,"%Y"))
    block[,2] <- as.integer(format(new.date,"%m"))
    block[,3] <- as.integer(format(new.date,"%d"))
    block[1,7] <- yesterdayObs
    block[2,6] <- fc[1]
    block[3,5] <- fc[2]
    block[4,4] <- fc[3]
    #print(block)

}else{

    cat('the data before adding\n')
    #print(block)
    colnames(block) <- header
    for(i in 0:nfcdays){    
        file.dates <- as.POSIXct(paste(block$year,block$mo,block$dy),tz="","%Y %m %d")
        this.date <- as.POSIXct(Sys.Date()+i)

        new.year <- as.integer(format(this.date,"%Y"))
        new.mon <- as.integer(format(this.date,"%m"))
        new.day <- as.integer(format(this.date,"%d"))

        this.comp.date <- as.POSIXct(paste(new.year,new.mon,new.day),tz="","%Y %m %d")
        in.file <- (file.dates == this.comp.date)
        
        cat(format(this.date,"%Y %m %d"))
        #print(in.file) 
        if(any(in.file)){
            #then the date is in the file so put the new value in
            ind <- (1:nrow(block))[in.file]
            if(length(ind)>1) stop("Two dates are the same in the file.")
            cat("\nadding value to row",ind,"\n")
            if(i==0){
                cat("value is",yesterdayObs,'\n')
                block[ind,7] <- yesterdayObs
            }else{
                cat("value is",fc[i],'\n')
                block[ind,(3+nfcdays+1)-i] <- fc[i]
            }
            print(block[ind,]);cat('\n')
            #print(block)
        }else{
            #then the date is not in the file so figure out
            #which rows to stick it in between or 
            #to add it on the end
            new.row <- c(new.year,new.mon,new.day,rep(NA,nfcdays+1))

            in.file <- (file.dates > this.date)
            #print(in.file)
            if(any(in.file)){
                #then stick it before row ind
                # so that the now row ind is the new row
                ind <- min((1:nrow(block))[in.file])
                
                block <- rbind(block[1:(ind-1),],new.row,block[ind:nrow(block),])
            }else{
                #then stick it at the end
                block <- rbind(block,new.row)
                ind <- nrow(block)
            }
            cat("\nadding value to NEW row",ind,"\n")
            if(i==0){ 
                cat("value is",yesterdayObs,'\n')
                block[ind,7] <- yesterdayObs
            }else{               
                cat("value is",fc[i],'\n')
                block[ind,(3+nfcdays+1)-i] <- fc[i]
            }
            print(block[ind,]);cat('\n')
            #print(block)
        }
    }
}

#the block should now have the new data added

if(use.gdata){
    #write.fwf(block, file=outfile,sep='  ', na="-999")
    cat(header,'\n',file=outfile)
    block[is.na(block)] <- -9
    write.table(cbind(sprintf("%4i",block[,1]),
                      sprintf("%2i",block[,2]),
                      sprintf("%2i",block[,3]),
                      matrix(sprintf("%8.2f",
                                 as.matrix(block)[,4:(3+nfcdays+1)]),
                      ncol=4)),
          file=outfile,
          append=TRUE,
          row.names = FALSE,
          col.names = FALSE,
          quote = FALSE)

}else{
    write.table(block, file = outfile, quote = FALSE, row.names = FALSE, na="-999")
}

